package ask.speedy.util;

import java.io.InputStream;
import java.util.Vector;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import ask.speedy.basic.BasicNode;
import ask.speedy.basic.Modem;
import ask.speedy.basic.TutorialVideo;

public class ContainerParser {
	
	public static BasicNode tree;
	public static Vector<Modem> modems = new Vector<Modem>();
	public static Vector<TutorialVideo> videos = new Vector<TutorialVideo>();
	private InputStream is, tutorial_is;

	public ContainerParser(InputStream is, InputStream tutorial_is) throws Exception{
		this.is = is;
		this.tutorial_is = tutorial_is;
		
		getQuestion();
		getTutorialContent();
		
	}

	private void getQuestion() throws Exception {
		/***
		 * document builder
		 */
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder = factory.newDocumentBuilder();
		Document document = builder.parse(is);
		
		Element rootElement = document.getDocumentElement();
		rootElement.normalize();
		
		tree = new BasicNode();
		
		//read xml for knowledge aquisition
		NodeList nodeList = rootElement.getElementsByTagName("askspeedy");
		if (nodeList.getLength() != 0) {
			//1 tag <askspeedy>
			Node askspeedy = nodeList.item(0);
			NodeList rootNodeLists = askspeedy.getChildNodes();
			for (int i = 0; i < rootNodeLists.getLength(); ++i) {
				Node item = rootNodeLists.item(i);
				if (item.getNodeType() == Node.ELEMENT_NODE) {
					//<node_0>
					getChildOfNodeTag(tree, item);
				}
			}
		}
	}
	
	private void getTutorialContent() throws Exception {
		
		/***
		 * document builder
		 */
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder = factory.newDocumentBuilder();
		Document document = builder.parse(tutorial_is);
		
		Element rootElement = document.getDocumentElement();
		rootElement.normalize();
		
		modems = new Vector<Modem>();
		
		//read xml for knowledge aquisition
		NodeList nodeList = rootElement.getElementsByTagName("tutorial");
		if (nodeList.getLength() != 0) {
			//1 tag <tutorial>
			Node askspeedy = nodeList.item(0);
			NodeList rootNodeLists = askspeedy.getChildNodes();
			for (int i = 0; i < rootNodeLists.getLength(); ++i) {
				Node item = rootNodeLists.item(i);
				if (item.getNodeType() == Node.ELEMENT_NODE) {
					getTutorialTag(item);
				}
			}
		}
	}
	
	private void getChildOfNodeTag(BasicNode parent, Node node) {
	//1st : <node_0>
		NodeList tagList = node.getChildNodes();
		for (int j = 0; j < tagList.getLength(); ++j) {
			
			Node aNode = tagList.item(j);
			if (aNode.getNodeType() == Node.ELEMENT_NODE) {
				
				Element elmtNode = (Element)aNode;
				String tagname = elmtNode.getTagName();
				
				if (tagname.equals("content")) {
					parent.setInformation(elmtNode.getTextContent());
				} else if (tagname.equals("image")) { 
					parent.setUrlImage(elmtNode.getTextContent());
				} else if (tagname.equals("answer")) {
					parent.setAnswer(elmtNode.getTextContent());
				} else if (tagname.equals("child")) {
					
					NodeList childList = aNode.getChildNodes(); //<node_level>
					for (int k = 0; k < childList.getLength(); ++k) {
						Node childNode = childList.item(k);
						if (childNode.getNodeType() == Node.ELEMENT_NODE) { //<node_level>
							BasicNode childCurrentNode = new BasicNode();
							getChildOfNodeTag(childCurrentNode, childNode);
							parent.addChild(childCurrentNode.getAnswer(), childCurrentNode);
							childCurrentNode.setParent(parent);
						}
					}
					
				}
			}
		}
	}
	
	private void getTutorialTag(Node node) {
		//1st : <field>
		NodeList tagList = node.getChildNodes();
		for (int j = 0; j < tagList.getLength(); ++j) {
			
			Node aNode = tagList.item(j);
			if (aNode.getNodeType() == Node.ELEMENT_NODE) {
				
				Element elmtNode = (Element)aNode;
				String tagname = elmtNode.getTagName();
				
				if (tagname.equals("modem")) {
					getAModem(aNode);
				}
				else if (tagname.equals("item")) { 
					getAVideo(aNode);
				}
			}
		}
	}
	
	private void getAVideo(Node aNode) {
		String title = null;
		String url = null;
		
		NodeList items = aNode.getChildNodes();
		for (int l = 0; l < items.getLength(); ++l) {
			Node item = items.item(l);
			
			if (item.getNodeType() == Node.ELEMENT_NODE) {
				Element elmtItem = (Element)item;
				String tagitem = elmtItem.getTagName();
				if (tagitem.equals("title")) {
					title = elmtItem.getTextContent();
				} else if (tagitem.equals("url")) {
					url = elmtItem.getTextContent();
				}
			}
		}
		
		TutorialVideo tutorialVideo = new TutorialVideo(title);
		tutorialVideo.setUrl(url);
		videos.add(tutorialVideo);
	}
	
	private void getAModem(Node aNode) {
				
		Element elmtModem = (Element)aNode;
					
		String name = elmtModem.getAttribute("name");
		Modem tutorial_modem = new Modem(name);
		NodeList itemList = aNode.getChildNodes();
		
		for (int l = 0; l < itemList.getLength(); ++l) { //1 <item>
			
			Node aItem = itemList.item(l);
			
			if (aItem.getNodeType() == Node.ELEMENT_NODE) {
				
				Element elmtItem = (Element)aItem;
				
				if (elmtItem.getTagName().equals("item")) {
					
					String title = null;
					
					Vector<String> contents = new Vector<String>();
					
					NodeList items = aItem.getChildNodes();
					for (int m = 0; m < items.getLength(); ++m) { //<title><content>
						
						Node item = items.item(m);
						
						if (item.getNodeType() == Node.ELEMENT_NODE) {
							
							Element elmtMember = (Element)item;
							String tagitem = elmtMember.getTagName();
							if (tagitem.equals("title")) {
								title = elmtMember.getTextContent();
							} else if (tagitem.equals("content")) {
								contents.add(elmtMember.getTextContent());
							}
						}
					}
					
					tutorial_modem.addModem_content(title, contents);
					
				}
			}
		}
		
		modems.add(tutorial_modem);
	}
}
